Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove buggy useEffect usage #1316

Merged
merged 6 commits into from
Dec 17, 2024
Merged

Conversation

daniel-heppner-ibigroup
Copy link
Contributor

Description:
These useEffect usages were causing bugs. Since useEffect calls its callback any time one of the dependencies change, it can be easy to accidentally introduce unexpected bugs. In this case, the query params changing was causing the dateTime effect to be called, which was setting the itinerary sort when external parameters were changing. In this case, we noticed changing the sort order from the itinerary header was causing the effect to run, immediately overriding the setting.

This is actually a good lesson in why useEffect is dangerous!

Copy link
Collaborator

@miles-grant-ibigroup miles-grant-ibigroup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All is working! Thanks for the cleanup

Copy link
Collaborator

@binh-dam-ibigroup binh-dam-ibigroup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you useCallback for the handler of the <input> onChange event, and see if adding relevant variables to the dependency arrays in the hooks?

lib/components/form/call-taker/date-time-picker.tsx Outdated Show resolved Hide resolved

// Update state when external state is updated
useEffect(() => {
if (initialDate !== date) setDate(initialDate)
if (initialTime !== time) {
setTime(initialTime)
handleTimeChange(initialTime || '')
}
}, [initialTime, initialDate])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint is highlighting missing dependencies here: date, time, and handleTimeChange, could you add them to the dependency array as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem is these useEffect hooks are propagating updates from the store into the component, adding those deps would cause a loop. Otherwise I would have just replaced them with callbacks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daniel-heppner-ibigroup Okay, could you add a comment about the undesired side effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one in caf809c

)
}
}, [time])

useEffect(() => {
if (initialDepartArrive && departArrive !== initialDepartArrive) {
setDepartArrive(initialDepartArrive)
}
}, [initialDepartArrive])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we are in fixing dependency arrays, could you adding departArrive in here too per eslint.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, causes a loop

}, [departArrive, setTime, setDate, homeTimezone])
}
},
[syncSortWithDepartArrive, sort, importedUpdateItineraryFilter]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, eslint is suggesting to add dateTime, handleTimeChange, homeTimezone to the dependency array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good note, added them.

Copy link
Collaborator

@binh-dam-ibigroup binh-dam-ibigroup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's give it a shot, thanks for the changes.

@daniel-heppner-ibigroup daniel-heppner-ibigroup merged commit 20a7092 into dev Dec 17, 2024
9 checks passed
@daniel-heppner-ibigroup daniel-heppner-ibigroup deleted the cleanup-calltaker-useeffect branch December 17, 2024 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants